package cn.anecansaitin.not_enough_harness.gen;

import cn.anecansaitin.not_enough_harness.NotEnoughHarness;
import cn.anecansaitin.not_enough_harness.client.ModKeymappings;
import cn.anecansaitin.not_enough_harness.common.ModItems;
import it.unimi.dsi.fastutil.Pair;
import net.minecraft.data.PackOutput;
import net.minecraft.world.item.Item;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.data.LanguageProvider;
import net.neoforged.neoforge.data.event.GatherDataEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

@EventBusSubscriber(modid = NotEnoughHarness.MOD_ID)
abstract class Lang extends LanguageProvider {
    private static final List<Pair<Supplier<? extends Item>, String>> zhcnItem = new ArrayList<>();
    private static final List<Pair<String, String>> zhcnKey = new ArrayList<>();
    private static final List<Pair<Supplier<? extends Item>, String>> enusItem = new ArrayList<>();
    private static final List<Pair<String, String>> enusKey = new ArrayList<>();


    static {
        addItem(ModItems.VERTILEAP_HARNESS, "擎跃挽具", "Vertileap Harness");
        addItem(ModItems.JET_HARNESS, "穿梭挽具", "Jet Harness");
        addItem(ModItems.CLOUD_WALKER_HARNESS, "云行挽具", "Cloud Walker Harness");
        addItem(ModItems.BOX_HARNESS, "箱子挽具", "Box Harness");
        addItem(ModItems.RUSH_HARNESS, "急袭挽具", "Rush Harness");
        addKey(ModKeymappings.HAPPY_GHAST_MOVE_DOWN.get().getName(), "快乐恶魂下降", "Happy Ghast Down");
        addKey(ModKeymappings.HARNESS_CAPABILITY.get().getName(), "挽具能力", "Harness Capability");
    }

    private static void addItem(Supplier<? extends Item> key, String zh, String en) {
        zhcnItem.add(Pair.of(key, zh));
        enusItem.add(Pair.of(key, en));
    }

    private static void addKey(String key, String zh, String en) {
        zhcnKey.add(Pair.of(key, zh));
        enusKey.add(Pair.of(key, en));
    }

    protected Lang(PackOutput output, String locale) {
        super(output, NotEnoughHarness.MOD_ID, locale);
    }

    private static class ZHCN extends Lang {
        protected ZHCN(PackOutput output) {
            super(output, "zh_cn");
        }

        @Override
        protected void addTranslations() {
            for (Pair<Supplier<? extends Item>, String> pair : zhcnItem) {
                addItem(pair.first(), pair.second());
            }

            for (Pair<String, String> pair : zhcnKey) {
                add(pair.first(), pair.second());
            }
        }
    }

    private static class ENUS extends Lang {
        protected ENUS(PackOutput output) {
            super(output, "en_us");
        }

        @Override
        protected void addTranslations() {
            for (Pair<Supplier<? extends Item>, String> pair : enusItem) {
                addItem(pair.first(), pair.second());
            }

            for (Pair<String, String> pair : enusKey) {
                add(pair.first(), pair.second());
            }
        }
    }

    @SubscribeEvent
    public static void gatherData(GatherDataEvent.Client event) {
        event.createProvider(ZHCN::new);
        event.createProvider(ENUS::new);
    }
}
